#!/usr/bin/env python
""" generated source for module EReader """
#
# Original file copyright original author(s).
# This file copyright Troy Melhase, troy@gci.net.
#
# WARNING: all changes to this file will be lost.

from gj.lib import Boolean, Double, DataInputStream, Integer, Long, StringBuffer, Thread
from gj.lib.overloading import overloaded


from gj.ext.EClientErrors import EClientErrors

from gj.ext.Util import Util

# 
#  * EReader.java
#  *
#  
# package: com.ib.client






class EReader(Thread):
    """ generated source for class EReader """
    #  incoming msg id's
    TICK_PRICE = 1
    m_parent = None
    m_dis = None

    def parent(self):
        """ generated source for method parent """
        return self.m_parent

    def eWrapper(self):
        """ generated source for method eWrapper """
        return self.parent().wrapper()

    @overloaded
    def __init__(self, parent, dis):
        """ generated source for method __init__ """
        self.__init__("EReader", parent, dis)

    @__init__.register(object, str, object, DataInputStream)
    def __init___0(self, name, parent, dis):
        """ generated source for method __init___0 """
        Thread.__init__(self, name, parent, dis)
        self.setName(name)
        self.m_parent = parent
        self.m_dis = dis

    def run(self):
        """ generated source for method run """
        try:
            #  loop until thread is terminated
            while not self.isInterrupted() and self.processMsg(self.readInt()):
                pass
        except Exception as ex:
            if self.parent().isConnected():
                self.eWrapper().error(ex)
        if self.parent().isConnected():
            self.m_parent.close()
        try:
            self.m_dis.close()
            self.m_dis = None
        except Exception as e:
            pass

    #  Overridden in subclass. 
    def processMsg(self, msgId):
        """ generated source for method processMsg """
        if msgId == -1:
            return False
        if msgId == self.TICK_PRICE:
           pass
        else:
            self.m_parent.error(EClientErrors.NO_VALID_ID, EClientErrors.UNKNOWN_ID.code(), EClientErrors.UNKNOWN_ID.msg())
            return False
        return True

    def readStr(self):
        """ generated source for method readStr """
        buf = StringBuffer()
        while True:
            c = self.m_dis.readByte()
            if c == 0:
                break
            buf.append(c)

        strval = str(buf)
        return None if 0 == len(strval) else strval

    def readBoolFromInt(self):
        """ generated source for method readBoolFromInt """
        strval = self.readStr()
        return False if strval is None else (Integer.parseInt(strval) != 0)

    def readInt(self):
        """ generated source for method readInt """
        strval = self.readStr()
        return 0 if strval is None else Integer.parseInt(strval)

    def readIntMax(self):
        """ generated source for method readIntMax """
        strval = self.readStr()
        return Integer.MAX_VALUE if (strval is None or 0 == len(strval)) else Integer.parseInt(strval)

    def readLong(self):
        """ generated source for method readLong """
        strval = self.readStr()
        return 0 if strval is None else Long.parseLong(strval)

    def readDouble(self):
        """ generated source for method readDouble """
        strval = self.readStr()
        return 0 if strval is None else Double.parseDouble(strval)

    def readDoubleMax(self):
        """ generated source for method readDoubleMax """
        strval = self.readStr()
        return Double.MAX_VALUE if (strval is None or 0 == len(strval)) else Double.parseDouble(strval)

